package com.kingsalert.kingsalert.analytics.bean

import com.google.gson.Gson
import com.kingsalert.kingsalert.analytics.util.AnalyticsUtil
import com.kingsalert.kingsalert.config.Constant
import com.kingsalert.kingsalert.util.AESCryptUtil
import com.kingsalert.kingsalert.util.UserInfoUtil
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

open class PamBase(
    /** product */
    var product: String = "",
    /** clientType */
    var clientType: String = "",
    /** aduidPath */
    var aduidPath: String = "",
    var path: String = "",
    /** token */
    var token: String = "",
    /** userId */
    var userId: String = "",
    /** 是否限制广告id，1是 0否 */
    var isLimitAdTrackingEnabled: String = "",
    /** 接口版本号 */
    var apiVersion: String = "v3",
    /** aduid */
    var aduid: String = "",
    /** androidId */
    var androidId: String = "",
    /** advertId */
    var advertId: String = "",
    /** version */
    var version: String = ""
) {
    init {
        this.advertId = AnalyticsUtil.getAdId()
        this.aduidPath = AnalyticsUtil.getAndroidId()
        this.token = UserInfoUtil.getInstance().token
        this.product = Constant.PRODUCT
        this.clientType = "ANDROID"
        this.isLimitAdTrackingEnabled = AnalyticsUtil.getAdEnabled().toString()
        this.aduid = AnalyticsUtil.getAdId()
        this.userId = UserInfoUtil.getInstance().userId
        this.version = Constant.APP_VERSION
        this.androidId = AnalyticsUtil.getAndroidId()
    }

    open fun toEncryptParams(path1: String): String {
        path = path1
        val content = Gson().toJson(this).trim()
        return AESCryptUtil.encryptDefault(content)
    }

    open fun toRequestBody(path: String): RequestBody {
        val encrypted: String = toEncryptParams(path)
        val mediaType = "text/plain".toMediaTypeOrNull()
        return encrypted.toRequestBody(mediaType)
    }
}